const App = {
    // mixins: [Query],//配置文件，路径/utils/mixins.js
    data() {
        return {
            treeData: [], //树的数据
            columns: [], //表格的列
            loading: false, //表数据的懒加载
            showDialog: false, //设置字段弹窗
            checkedFields: [], //选中的字段
            Tabletdata: [], //表格的数据
            fields: [], //字段列表
            Total: 0,
            page: {
                value1: '',
                value2: '',
                PageNumber: 1,
                RowAmount: 10,
                Condition: window.btoa(unescape(encodeURIComponent("%%,%%,%%"))),
            },
            key: '',
            relativePath: '',
        }
    },
    mounted() {
        var currentURL = window.location.href;
        this.relativePath = currentURL.substring(
            currentURL.lastIndexOf("view"),
            currentURL.length
        );
        this.Treedata();
        this.getList();
    },
    computed: {
        // 计算属性，返回选中的字段
        isAllChecked() {
            return this.checkedFields.length === this.fields.length;
        },
    },
    methods: {
        handleSizeChange(newSize) {
            this.page.PageNumber = 1;
            this.page.RowAmount = newSize;
            this.postgetList(this.page);
        },
        handleCurrentChange(newPage) {
            this.page.PageNumber = newPage;
            this.postgetList(this.page);
        },
        check() {
            this.page.Condition = window.btoa(unescape(encodeURIComponent("%%,%" + this.page.value1 + "%,%" + this.page.value2 + "%")));
            this.postgetList(this.page)
        },
        //树的数据
        Treedata() {
            let { TableName, NodeCode } = JSON.parse(document.getElementById("tree").getAttribute("data-options"));
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/getPartyOrganizationTree`,
                type: "GET",
                data: {
                    TableName,
                    NodeCode
                },
                success: (data) => {
                    this.treeData = data; // 直接使用 this
                },
                complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json",
            });
        },
        //添加按钮 树
        personalAdds(row) {
            let { primary_key } = JSON.parse(document.getElementById("tree").getAttribute("data-options"));
            window.location.href = "./addtree.html?action=Add" + (row ? "&nodeKey=" + row[primary_key] : "");
        },
        //编辑按钮 树
        handleEdits(row) {
            let { primary_key } = JSON.parse(document.getElementById("tree").getAttribute("data-options"));
            window.location.href = "./addtree.html?action=Modify&nodeKey=" + row[primary_key];
        },
        // 树删除
        remove(node, data) {
            this.$confirm('该操作将删除此条数据,是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
            }).then(() => {
                data.Row_Status = '1';
                let { TableName } = JSON.parse(document.getElementById("tree").getAttribute("data-options"));
                let lJSONObject_DataSet = new Object();
                let lJSONArray_Record = new Array();
                lJSONArray_Record.push(data);
                let lJSONObject_Table = new Object();
                lJSONObject_Table[TableName] = lJSONArray_Record;
                lJSONObject_DataSet['Modify'] = lJSONObject_Table;
                let Operation_Main = JSON.stringify(deepClone(lJSONObject_DataSet));
                if (!lJSONObject_DataSet['Add']) {
                    lJSONObject_DataSet['Add'] = new Object();
                }
                lJSONObject_DataSet['Add']['base_operation_log'] = [{
                    Operation_Type: '0',
                    Operation_Path: this.relativePath,
                    Operation_Main,
                    Operation_Interface: "generalSave",
                    Operation_IP: getIpAddress()
                }]
                submitSave(lJSONObject_DataSet, (res) => {
                    if (res.Status == 'success') {
                        this.Treedata();
                    } else {
                        this.$message({
                            type: 'error',
                            message: '删除失败'
                        });
                    }
                })
                // $.ajax({
                //     headers: requestHeaders(),
                //     url: `${mh}/generalBusiness/generalSave`,
                //     method: "post",
                //     data: {
                //         Data: JSON.stringify(lJSONObject_DataSet),
                //     },
                //     success: (res) => {
                //         if (res.Status == 'success') {
                //             this.Treedata();
                //         } else {
                //             this.$message({
                //                 type: 'error',
                //                 message: '删除失败'
                //             });
                //         }
                //     },
                //     complete: (XMLHttpRequest) => {
                //         requestPretreatment(XMLHttpRequest);
                //     },
                // });
            }).catch(() => { });
        },
        //树的点击筛选事件
        handleNodeClick(data, node) {
            this.page.Condition = window.btoa(unescape(encodeURIComponent("%" + data.Party_Organization_SerialNumber + "%,%" + this.page.value1 + "%,%" + this.page.value2 + "%")));
            this.postgetList(this.page)
        },
        //获取表格的列
        getList() {
            const that = this;
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryBySQLSerialNumber`,
                method: "post",
                data: {
                    SQLSerialNumber: 's_base_sql_table_config',
                    Condition: window.btoa(unescape(encodeURIComponent("%" + this.relativePath + "%"))),
                    ExtensionCondition: '',
                },
                success: (data) => {
                    that.columns = data;
                    that.checkedFields = data;
                    that.fields = data;
                    that.loading = true;
                    that.postgetList(that.page);
                },
                complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest);
                },
            });
        },
        //获取表格的数据
        postgetList(page) {
            const { TableName } = JSON.parse(document.getElementById("table").getAttribute("data-options"));
            const that = this;
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalPaginationQueryBySQLSerialNumber`,
                method: "post",
                data: {
                    Model_Url: this.relativePath,
                    Condition: page.Condition1,
                    PaginationNumber: page.PageNumber,
                    ...page
                },
                success: (res) => {
                    that.Total = res.TotalRows;//总数量
                    that.loading = false;
                    that.Tabletdata = res.Rows;
                    submitSave({
                        Add: {
                            base_operation_log: [{
                                Operation_Type: '0',
                                Operation_Path: this.relativePath,
                                Operation_Main: JSON.stringify(deepClone({
                                    Data: res.Rows,
                                    Param: {
                                        Model_Url: this.relativePath,
                                        Condition: page.Condition1,
                                        PaginationNumber: page.PageNumber,
                                        ...page
                                    }
                                })),
                                Operation_Interface: "generalPaginationQueryBySQLSerialNumber",
                                Operation_IP: getIpAddress()
                            }]
                        }
                    })
                    // $.ajax({
                    //     headers: requestHeaders("JSON"),
                    //     url: `${mh}/generalBusiness/generalSaveByRequestBody`,
                    //     method: "post",
                    //     data: JSON.stringify({
                    //         Data: {
                    //             Add: {
                    //                 base_operation_log: [{
                    //                     Operation_Type: '0',
                    //                     Operation_Path: this.relativePath,
                    //                     Operation_Main: JSON.stringify(deepClone({
                    //                         Data: res.Rows,
                    //                         Param: {
                    //                             Model_Url: this.relativePath,
                    //                             Condition: page.Condition1,
                    //                             PaginationNumber: page.PageNumber,
                    //                             ...page
                    //                         }
                    //                     })),
                    //                     Operation_Interface: "generalPaginationQueryBySQLSerialNumber",
                    //                     Operation_IP: getIpAddress()
                    //                 }]
                    //             }
                    //         },
                    //     }),
                    //     complete: (XMLHttpRequest) => {
                    //         requestPretreatment(XMLHttpRequest)
                    //     }
                    // });
                },
                complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest);
                },
            });
        },
        //添加按钮
        personalAdd() {
            window.location.href = "./add.html?action=Add&index=0";
        },
        //编辑按钮
        handleEdit(row) {
            let { primary_key } = JSON.parse(document.getElementById("table").getAttribute("data-options"));
            window.location.href = "./steps.html?nodeKey=" + row[primary_key];
        },
        handleShow(row) {
            let { primary_key } = JSON.parse(document.getElementById("table").getAttribute("data-options"));
            window.location.href = "./show.html?action=Modify&nodeKey=" + row[primary_key];
        },
        //删除数据
        handleDelete(data) {
            this.$confirm('该操作将删除此条数据,是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
            }).then(() => {
                let { TableName, primary_key } = JSON.parse(document.getElementById("table").getAttribute("data-options"));
                $.ajax({
                    headers: requestHeaders(),
                    url: `${mh}/generalBusiness/generalQueryByTableNameWithOriginal`,
                    method: "post",
                    data: {
                        Data: TableName,
                        Condition: primary_key + "='" + data[primary_key] + "'"
                    },
                    success: (res) => {
                        if (res.length == 1) {
                            res[0].Row_Status = '99';
                            let lJSONObject_DataSet = new Object();
                            let lJSONObject_Table = new Object();
                            lJSONObject_Table[TableName] = res;
                            lJSONObject_DataSet['Modify'] = lJSONObject_Table;
                            let Operation_Main = JSON.stringify(deepClone(lJSONObject_DataSet));
                            if (!lJSONObject_DataSet['Add']) {
                                lJSONObject_DataSet['Add'] = new Object();
                            }
                            lJSONObject_DataSet['Add']['base_operation_log'] = [{
                                Operation_Type: '0',
                                Operation_Path: this.relativePath,
                                Operation_Main,
                                Operation_Interface: "generalSave",
                                Operation_IP: getIpAddress()
                            }]
                            submitSave(lJSONObject_DataSet,(res)=>{
                                if (res.Status == 'success') {
                                    this.postgetList(this.page);
                                } else {
                                    this.$message({
                                        type: 'error',
                                        message: '删除失败'
                                    });
                                }
                            })
                            // $.ajax({
                            //     headers: requestHeaders(),
                            //     url: `${mh}/generalBusiness/generalSave`,
                            //     method: "post",
                            //     data: {
                            //         Data: JSON.stringify(lJSONObject_DataSet),
                            //     },
                            //     success: (res) => {
                            //         if (res.Status == 'success') {
                            //             this.postgetList(this.page);
                            //         } else {
                            //             this.$message({
                            //                 type: 'error',
                            //                 message: '删除失败'
                            //             });
                            //         }

                            //     },
                            //     complete: (XMLHttpRequest) => {
                            //         requestPretreatment(XMLHttpRequest);
                            //     },
                            // });
                        } else {
                            this.$message({
                                type: 'error',
                                message: '删除失败'
                            });
                        }

                    },
                    complete: (XMLHttpRequest) => {
                        requestPretreatment(XMLHttpRequest);
                    },
                });
            }).catch(() => { });
        },
        handleSelectionChange() { },
        //查询按钮

        //重置按钮
        reset() {
            // 这里可以根据你的实际需求修改逻辑
            window.location.reload();
        },
        //过滤树的数据
        filterTreeData(data, searchText) {
            return data.filter((item) => {
                if (item.label.includes(searchText)) {
                    return true;
                }
                if (item.children) {
                    const filteredChildren = this.filterTreeData(
                        item.children,
                        searchText
                    );
                    if (filteredChildren.length > 0) {
                        item.children = filteredChildren;
                        return true;
                    }
                }
                return false;
            });
        },
        // 弹窗全选
        handleAllChecked(val) {
            let a; // 提前声明变量 a
            if (val === false) {
                console.log(this.fields);
                a = this.fields.filter((item) => {
                    return item.COLUMN_KEY == "PRI";
                });
                console.log(a);
            } else {
                a = [...this.fields]; // 当 val 为 true 时，给 a 赋值为 props.fields 的副本
            }
            // 使用 emit 更新 checkedFields
            this.checkedFields = a;
        },
        saveFields() {
            const selectedFields = this.fields.filter((item) => {
                return this.checkedFields.some(
                    (cItem) => cItem && cItem.COLUMN_NAME === item.COLUMN_NAME
                );
            });
            console.log("选中的字段:", selectedFields);
            // 更新 columns
            this.columns = selectedFields;
            this.showDialog = false;
        },
        //显示字段弹窗
        handleOpenDialog() {
            this.showDialog = true;
        },
    },
};

const { createApp } = Vue;
const app = createApp(App);
//这种方式是引入全部图标，无需单个引入
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
    app.component(key, component);
}
const ElMessage = ElementPlus.ElMessage;
app.use(ElementPlus, { locale: zhCn });
app.mount("#app");
